package com.findu.control;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.findu.R;
import com.findu.model.FUFamilyMember;
import com.findu.model.FUFamilyTree;
import com.findu.model.FUFamilytreeMember;
import com.findu.model.FUMateTree;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

public class RelativesListAdapter extends BaseExpandableListAdapter {

	class RelativeItem{
		

		private String id;
		private String surname;
		private String name;
		private int sex;
		
		RelativeItem(FUFamilyMember member){
			id = member.id;
			surname = member.surname;
			name = member.name;
			sex = member.sex;
		}
		
		RelativeItem(FUFamilytreeMember member){
			id = member.getFu_userId();
			surname = member.getMemberSurname();
			name = member.getMemberName();
			sex = member.getMemberSex();
		}
		
		public String getId() {
			return id;
		}

		public String getSurname() {
			return surname;
		}

		public String getName() {
			return name;
		}

		public int getSex() {
			return sex;
		}
	}
	
	private LayoutInflater inflate = null;
	private HashMap<String, ArrayList<RelativeItem>> data = null;
	private ArrayList<ArrayList<RelativeItem>> listData = null;
	private ArrayList<String> listKey = null;
	
	public RelativesListAdapter(Context ctx){
		inflate = LayoutInflater.from(ctx);
	}
	
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		listData = new ArrayList<ArrayList<RelativeItem>>();
		listData.addAll(data.values());
		listKey = new ArrayList<String>();
		listKey.addAll(data.keySet());
		return data == null ? 0 : data.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return listData.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return listKey.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return listData.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			convertView = inflate.inflate(R.layout.relative_group_view, null);
		}
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			convertView = inflate.inflate(R.layout.relative_member_view, null);
		}
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

	public void setFamilyTree(FUFamilyTree familyTree) {
		RelativeItem item = new RelativeItem(familyTree.homeOwner);
		addMember(item);
		
		for(FUMateTree mate : familyTree.mates){
			item = new RelativeItem(mate.mate);
		}
		addMember(item);
		
		for(FUFamilyTree child : familyTree.childrens){
			setFamilyTree(child);
		}
	}
	
	private void addMember(RelativeItem member){
		ArrayList<RelativeItem> members;
		if(data.containsKey(member.getSurname())){
			members = data.get(member.getSurname());
		}else{
			members = new ArrayList<RelativeItem>();
		}
		members.add(member);
	}

}
